﻿@model PollModel
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)

<div id="poll-edit">
    <ul>
        <li @Html.RenderSelectedTabIndex(0, GetSelectedTabIndex())>
            @T("Admin.ContentManagement.Polls.Info")
        </li>
        <li @Html.RenderSelectedTabIndex(1, GetSelectedTabIndex())>
            @T("Admin.ContentManagement.Polls.Answers")
        </li>
    </ul>
    <div>
        @TabInfo()
    </div>
    <div>
        @TabAnswers()
    </div>
</div>
<script>
    $(document).ready(function () {
        $("#poll-edit").kendoTabStrip({
            animation: {
                open: {
                    effects: "fadeIn"
                }
            },
            select: tabstrip_on_tab_select
        });
    });
</script>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "poll-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}


@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
    {
        
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.LanguageId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.LanguageId, new SelectList(ViewBag.AllLanguages, "Id", "Name"))
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Name):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SystemKeyword):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SystemKeyword)
                @Html.ValidationMessageFor(model => model.SystemKeyword)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Published):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Published)
                @Html.ValidationMessageFor(model => model.Published)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.ShowOnHomePage):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.ShowOnHomePage)
                @Html.ValidationMessageFor(model => model.ShowOnHomePage)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AllowGuestsToVote):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AllowGuestsToVote)
                @Html.ValidationMessageFor(model => model.AllowGuestsToVote)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DisplayOrder):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DisplayOrder)
                @Html.ValidationMessageFor(model => model.DisplayOrder)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.StartDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.StartDate)
                @Html.ValidationMessageFor(model => model.StartDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.EndDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.EndDate)
                @Html.ValidationMessageFor(model => model.EndDate)
            </td>
        </tr>
    </table>
}
@helper TabAnswers()
    {
        if (Model.Id > 0)
        {
    <table class="adminContent">
        <tr>
            <td>
                <div id="answers-grid"></div>

                <script>
                    $(document).ready(function () {
                        $("#answers-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("PollAnswers", "Poll", new { pollId = Model.Id }))",
                                        type: "POST",
                                        dataType: "json"
                                    },
                                    create: {
                                        url: "@Html.Raw(Url.Action("PollAnswerAdd", "Poll", new { pollId = Model.Id }))",
                                        type: "POST",
                                        dataType: "json"
                                    },
                                    update: {
                                        url:"@Html.Raw(Url.Action("PollAnswerUpdate", "Poll"))",
                                        type: "POST",
                                        dataType: "json"
                                    },
                                    destroy: {
                                        url: "@Html.Raw(Url.Action("PollAnswerDelete", "Poll"))",
                                        type: "POST",
                                        dataType: "json"
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors",
                                    model: {
                                        id: "Id",
                                        fields: {
                                            Name: { editable: true, type: "string" },
                                            NumberOfVotes: { editable: false, type: "number" },
                                            DisplayOrder: { editable: true, type: "number" },
                                            Id: { editable: false, type: "number" }
                                        }
                                    }
                                },
                                requestEnd: function (e) {
                                    if (e.type == "create" || e.type == "update") {
                                        this.read();
                                    }
                                },
                                error: function (e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                numeric: false,
                                previousNext: false,
                                info: false
                            },
                            toolbar: ["create"],
                            editable: {
                                confirmation: false,
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [{
                                field: "Name",
                                title: "@T("Admin.ContentManagement.Polls.Answers.Fields.Name")",
                                width: 300
                            }, {
                                field: "NumberOfVotes",
                                title: "@T("Admin.ContentManagement.Polls.Answers.Fields.NumberOfVotes")",
                                width: 100
                            }, {
                                field: "DisplayOrder",
                                title: "@T("Admin.ContentManagement.Polls.Answers.Fields.DisplayOrder")",
                                //integer format
                                format: "{0:0}",
                                width: 100
                            }, {
                                command: [{
                                    name: "edit",
                                    text: "@T("Admin.Common.Edit")"
                                }, {
                                    name: "destroy",
                                    text: "@T("Admin.Common.Delete")"
                                }],
                                width: 200
                            }]
                        });
                    });

                </script>
            </td>
        </tr>
    </table>
        }
        else
        {
    @T("Admin.ContentManagement.Polls.Answers.SaveBeforeEdit")
        }
}